package com.ruoyi.oiltank.controller;

import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import com.ruoyi.wms.domain.WmsItem;
import net.sf.jsqlparser.statement.select.KSQLWindow;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.oiltank.domain.OilTank;
import com.ruoyi.oiltank.service.IOilTankService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 储油罐信息Controller
 * 
 * @author ruoyi
 * @date 2024-09-21
 */
@RestController
@RequestMapping("/oiltank/tank")
public class OilTankController extends BaseController
{
    @Autowired
    private IOilTankService oilTankService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    RedisCache redisCache;
    /**
     * 查询储油罐信息列表
     */
    @PreAuthorize("@ss.hasPermi('oiltank:tank:list')")
    @GetMapping("/list")
    public TableDataInfo list(OilTank oilTank)
    {

        startPage();
        List<OilTank> list = oilTankService.selectOilTankList(oilTank);
        return getDataTable(list);
    }
    /**
     * 查询储油罐信息列表
     */
    @GetMapping("/listNoPage")
    public TableDataInfo listNoPage(OilTank oilTank)
    {


        if (ObjectUtils.isEmpty(oilTank)){
            oilTank=new OilTank();
        }
        Boolean auth = oilTank.getAuth();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (ObjectUtil.isNotEmpty(auth)&&auth){
            Long deptId = loginUser.getUser().getDeptId();
            oilTank.setDeptId(deptId);
        }
        List<OilTank> list = oilTankService.selectOilTankList(oilTank);
        return getDataTable(list);
    }


    /**
     * 导出储油罐信息列表
     */
    @PreAuthorize("@ss.hasPermi('oiltank:tank:export')")
    @Log(title = "储油罐信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,OilTank oilTank)
    {
        //查询出所有部门
        List<SysDept> sysDepts = deptService.selectDeptList(new SysDept());
        SysDictData dict = new SysDictData();
        dict.setDictType("oil_tank_type");
        List<SysDictData> sysDictData = dictDataService.selectDictDataList(dict);
        dict.setDictType("oil_tank_status");
        List<SysDictData> tankStatus = dictDataService.selectDictDataList(dict);
        List<OilTank> list = oilTankService.selectOilTankList(oilTank);
        for (OilTank tank : list) {
            Long deptId = tank.getDeptId();
            Long id = tank.getTypeId();
            Integer status = tank.getStatus();
            SysDept correspondingDept = sysDepts.stream()
                    .filter(dept -> dept.getDeptId().equals(deptId))
                    .findFirst()
                    .orElse(null);
            SysDictData correspondingDict = sysDictData.stream()
                    .filter(d -> d.getDictValue().equals(String.valueOf(id)))
                    .findFirst()
                    .orElse(null);
            SysDictData correspondingStatus = tankStatus.stream()
                    .filter(d -> d.getDictValue().equals(String.valueOf(status)))
                    .findFirst()
                    .orElse(null);


            tank.setDeptName(ObjectUtil.isNotNull(correspondingDept)?correspondingDept.getDeptName():"");
            tank.setTypeName(ObjectUtil.isNotNull(correspondingDict)?correspondingDict.getDictLabel():"");
            tank.setStatusName(ObjectUtil.isNotNull(correspondingStatus)?correspondingStatus.getDictLabel():"");


        }
        ExcelUtil<OilTank> util = new ExcelUtil<OilTank>(OilTank.class);
         util.exportExcel(response,list, "储油罐信息数据");
    }

    /**
     * 获取储油罐信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('oiltank:tank:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(oilTankService.selectOilTankById(id));
    }

    /**
     * 新增储油罐信息
     */
    @PreAuthorize("@ss.hasPermi('oiltank:tank:add')")
    @Log(title = "储油罐信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OilTank oilTank)
    {
        return toAjax(oilTankService.insertOilTank(oilTank));
    }

    /**
     * 修改储油罐信息
     */
    @PreAuthorize("@ss.hasPermi('oiltank:tank:edit')")
    @Log(title = "储油罐信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OilTank oilTank)
    {
        return toAjax(oilTankService.updateOilTank(oilTank));
    }

    /**
     * 删除储油罐信息
     */
    @PreAuthorize("@ss.hasPermi('oiltank:tank:remove')")
    @Log(title = "储油罐信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(oilTankService.deleteOilTankByIds(ids));
    }
}
